package ast.stmt;

import icg.Quadruple;
import ts.SymbolTable;
import ast.Node;
import ast.exp.Expression;
import ast.type.Type;

public class WriteStmt extends Statement {
	private Expression exp;
	private Boolean breakLine;

	public WriteStmt(Expression exp, Boolean breakLine) {
		this.exp = exp;
		this.setBreakLine(breakLine);
	}

	public void setBreakLine(Boolean breakLine) {
		this.breakLine = breakLine;
	}

	public Boolean getBreakLine() {
		return breakLine;
	}

	@Override
	public String toString() {
		if (breakLine)
			return "<WritelnStmt>\n" + exp + "\n</WritelnStmt>";
		return "<WriteStmt>\n" + exp + "\n</WriteStmt>";
	}

	@Override
	public void typeCheck(Type type) {
		Type typeExp = exp.typeCheck();
		if (!(typeExp == Type.STRING || typeExp == Type.INTEGER)) {
			System.out.println("invalid expression on Write statement");
			SymbolTable.errors++;
		}
	}

	@Override
	public void genCode() {
		exp.genCode();
		Node.icg.gen(Quadruple.write(exp.addr));
	}
}
